<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // 用递归写5！
    // 求累加的和
    const factorial = (n) => {
      if (n === 1) {
        return 1;
      } else {
        return factorial(n - 1) + n; // 5*
      }
    }

    // 如果递归调用太多次，就会导致栈的溢出， 爆栈
    // caught RangeError: Maximum call stack size exceeded
    // 递归的优化  扩展-下！！
    let sum = factorial(12000);
    console.log(sum);

    // factorial(5);
    // factorial(4) * 5
    // factorial(3) * 4
    // factorial(2) * 3

    // factorial(1) * 2

    // 1

        //javascript尾递归优化 https://blog.csdn.net/hellocoder2029/article/details/127449024
  </script>
</body>

</html>